<!DOCTYPE html>
<html lang='zh-CN'>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
    <title>驿站分类管理</title>
    <link rel="icon" href="favicon.ico" type="image/ico">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/materialdesignicons.min.css" rel="stylesheet">
    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.min.css" rel="stylesheet">
    <link href="js/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
    <link rel="stylesheet" href="js/ztree/zTreeStyle/zTreeStyle.css" type="text/css">
    <link rel="stylesheet" href="js/dropzone/min/basic.min.css" />
    <link rel="stylesheet" href="js/dropzone/min/dropzone.min.css" />
</head>
<body>
<div class="container-fluid p-t-15">


    <div class="row">
        <div class="col-md-3">
            <div class="card">
                <div class="card-header">
                    <h4>驿站分类管理</h4>
                    <div>
                        <button class="btn btn-success btn-xs btn-round" id="addBtn">增加
                        </button>
                        <button class="btn btn-info btn-xs btn-round" id="editBtn">编辑</button>
                        <button class="btn btn-danger btn-xs btn-round"  id="delBtn">删除</button>
                    </div>
                </div>
                <div class="card-body">

                    <div class="zTreeDemoBackground left">
                        <ul id="tree" class="ztree"></ul>
                    </div>

                </div>
            </div>
        </div>
        <div class="col-md-9">
            <div class="card">
                <div class="card-header">
                    <h4>驿站商品</h4>
                </div>
                <div class="card-body">
                    <form id="searchForm">
                        <input id="ids" name="ids" type="hidden" value="">
                    </form>
                    <table id="tb_departments"></table>

                </div>
            </div>
        </div>

    </div>


    <!--  添加与编辑模态框 -->
    <div class="modal fade" id="addOrEditModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="exampleModalLabel">驿站分类管理</h4>
                </div>
                <div class="modal-body">
                    <form id="addOrEditForm" action="" method="post" data-target="validator">
                        <div class="form-group">
                            <label for="name" class="control-label">分类名称：</label>
                            <input type="hidden" name="id">
                            <input type="text" class="form-control" id="name" name="name">
                        </div>
                        <div class="form-group">
                            <label for="sort" class="control-label">排序：</label>
                            <input type="text" class="form-control" id="sort" name="sort">
                        </div>
                        <div class="form-group">
                            <label for="picture" class="control-label">上传图标：</label>
                            <input type="hidden" name="picture" id="picture">
                            <div id="example-dropzone"  class="dropzone"></div>
                        </div>

                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                            <button type="button" id="sava" class="btn btn-primary">保存</button>
                        </div>
                    </form>
                </div>

            </div>
        </div>
    </div>

</div>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/bootstrap-table/bootstrap-table.js"></script>
<script type="text/javascript" src="js/bootstrap-table/bootstrap-table-zh-CN.min.js"></script>
<script type="text/javascript" src="js/zero2one/zero2one.version.js"></script>
<script type="text/javascript" src="js/zero2one/zero2one.core.js"></script>
<!-- bootstrap表单验证插件 -->
<script type="text/javascript" src="js/bootstrap-validator-master/dist/validator.js"></script>
<!-- jquery-form表单提交插件 -->
<script type="text/javascript" src="js/jquery-form/jquery.form.js"></script>
<!-- bootstrap弹出框插件 -->
<script type="text/javascript" src="js/jquery.bootstrap.min.js"></script>
<!-- 表单填充插件 -->
<script type="text/javascript" src="js/jquery-form/jquery.formloader.js"></script>
<!-- ztree树插件 -->
<script type="text/javascript" src="js/ztree/jquery.ztree.core.min.js"></script>
<script type="text/javascript" src="js/zero2one/zero2one.ztree.js"></script>

<script type="text/javascript" src="js/bootstrapvalidator/dist/js/bootstrapValidator.js"></script>
<script type="text/javascript" src="js/dropzone/min/dropzone.min.js"></script>
<!--上传图片插件-->
<script type="text/javascript" src="js/zero2one/zero2one.drop.js"></script>
<script type="text/javascript" src="js/inputmoney.js"></script>

<script type="text/javascript">

    //页面初始化完成加载部门数据
    $.post("/remote/BusinCates/categoryBusiness", {},
        function(data){
            $.fn.zTree.init($("#tree"), {callback: {onClick: onClick}}, data);
    }, "json");

    var flag;//判断是否是顶级部门
    var zj;
    var ids;

    function onClick(event, treeId, treeNode) {
        zj = {id:treeNode.id, name:treeNode.name, sort:treeNode.sort,picture:treeNode.picture};
        ids = zero2one.getChildNodes("tree", treeNode);
        $("#ids").val(ids);
        getDeptUserByIds();
    }

    //上传图片
   zero2one.drop("example-dropzone","picture");

    function getDeptUserByIds() {
        //设置表头
        var columns = [
            {checkbox: true},
            {field: 'businessId',title: '商店id'},
            {field: 'businessName',title: '商家名称'},
            {field: 'productName',title: '商品名称'},
            {field: 'productStock',title: '商品当前库存'},
            {field: 'soldStock',title: '已售数量'},
            {field: 'subGraphList',title: '商品图片',formatter: function (value, row, index) {
                    console.log(value);
                    var v = JSON.parse(value);
                    if(v!=null&&v!=''){
                        var html='';
                        for (let i = 0; i < v.length; i++) {
                            html="&nbsp;&nbsp;<img style='width: 60px;height: 60px; border:1px solid #2ba3f6' src='"+v[0]+"'/>";
                        }
                        return html;
                    }else {
                        return "无图片"
                    }
                }}

        ];

        //进入主界面加载数据
        zero2one.datagrid("tb_departments", "/remote/BusinCate/getByIds",function (params) {
            return $.extend(zero2one.tableInitParam(params), zero2one.form2json("searchForm"));
        }, columns);

        $('#tb_departments').bootstrapTable('refresh');
    }


    //点击编辑按钮弹出模态框
    $("#editBtn").click(function () {
        //	判断是否选中当前要编辑的部门
        if(zj == null){
            $.messager.alert("温馨提示", "请先选择要编辑的分类再操作");
            return;
        }

        $("#example-dropzone").empty();
        var dropz = Dropzone.forElement('#example-dropzone');//获取Dropzone元素节点
        //创建模拟文件
        var mockFile = {
            name:zj.picture , //显示的文件名
            accepted:true
        };
        dropz.emit("addedfile", mockFile);
        //图片地址
        var imageUrl =zj.picture;
        //生成缩略图
        dropz.emit("thumbnail", mockFile,imageUrl);
        //完成（如果没有，缩略图上会有一根进度条一直存在）
        dropz.emit("complete", mockFile);

        //加载表单数据
        $("input[name=id]").val(zj.id);
        $("input[name=sort]").val(zj.sort);
        $("input[name=name]").val(zj.name);
        $("input[name=picture]").val(imageUrl);
        $("#addOrEditModal").modal("show");
    });

    //点击添加按钮弹出模态框
    $("#addBtn").click(function () {
        $("#example-dropzone").empty();
        $("input[name=id]").val("");
        $("input[name=sort]").val("");
        $("input[name=name]").val("");
        $("#addOrEditModal").modal("show");
    });

    //保存按钮的点击事件
    $("#sava").click(function () {
        zero2one.request("/remote/BusinCates/addOrEditBusiness",$("#addOrEditForm").serialize(),function (data) {
            if (data.success) {
                window.location.reload();
            }else {
                $.messager.alert("温馨提示", data.msg)
            }
        });
    });

    //删除
    $("#delBtn").click(function () {

        if(zj == null){
            $.messager.alert("温馨提示", "请选择一条数据操作");

        }else{
            $.messager.confirm("温馨提示", "您确定要删除"+zj.name+"分类吗？", function () {
                zero2one.request("/remote/BusinCates/delById",{"id": zj.id},function (data) {
                    if(data.success==false){
                        $.messager.alert(data.msg);

                    }else {
                        window.location.reload();
                    }

              });

            })
        }
    });

</script>

</body>
</html>
